package ;

/**
 * ...
 * @author 
 */

enum JagItemState
{
	Born;
	Idle;
	Moving;
	Dying;
	Dead;
}


class JagTileInfo extends TileInfo
{
	// types
	public inline static var JAG_Background: Int = 1;
	public inline static var JAG_Wall: Int = 2;
	public inline static var JAG_Item: Int = 4;
	
	// idents
	public inline static var Background1: Int = 0;
	public inline static var Wall1: Int = 1;
	public inline static var Target1: Int = 2;
	public inline static var Target2: Int = 3;

	public inline static var Item1: Int = 10;
	public inline static var Item2: Int = 11;
	public inline static var Item3: Int = 12;
	public inline static var Item4: Int = 13;
	public inline static var Item5: Int = 14;
	public inline static var Item6: Int = 15;
	public inline static var ItemMax: Int = 15;
	
	// state
	var mState: JagItemState;
	var mFrameCount: Int;
	var mStateFrames: Int;
	
	
	public inline static var BornTime = 6;
	public inline static var DeathTime = 4;
	public inline static var MoveTime = 2;
	
	
	public var fRemove(default, default): Bool;

	
	public function new()
	{
		super();
		
		mState = JagItemState.Idle;
		mFrameCount = 0;
		mStateFrames = 0;
		
		fRemove = false;
	}
	
	
	inline public function GetStateDoneFrames(): Int
	{
		return mStateFrames - mFrameCount;
	}
	
	
	inline public function GetStateFrames(): Int
	{
		return mStateFrames;
	}	
	
	
	inline public function GetState(): JagItemState
	{
		return mState;
	}

	
	inline public function IsFreeForItem(): Bool
	{
		return (mType & (JAG_Wall | JAG_Item) == 0);
	}

	
	inline public function IsDead(): Bool
	{
		return (mType & JAG_Item != 0) && (mState == JagItemState.Dead);
	}
	
	
	inline public function IsMoving(): Bool
	{
		return (mType & JAG_Item != 0) && (mState == JagItemState.Moving);		
	}
	
	
	public function CanBeRemoved(): Bool
	{
		if (mType & JAG_Item == 0)
			return false;
		
		if (mState == JagItemState.Idle)
			return true;
			
		return false;
	}

	
	public function CanBeMoved(): Bool
	{
		if (mType & JAG_Item == 0)
			return false;
		
		if (mState == JagItemState.Idle)
			return true;
			
		return false;
	}
	
	
	public function CreateItem(itemId: Int)
	{
		SetType(JagTileInfo.JAG_Item, true);
		SetId(itemId);

		mState = JagItemState.Born;
		mStateFrames = BornTime;
		mFrameCount = BornTime;
	}
	
	
	public function KillItem()
	{
		mState = JagItemState.Dying;
		mFrameCount = DeathTime;
		mStateFrames = DeathTime;
		
		// unschedule removal
		fRemove = false;		
	}
	
	
	public function MoveTo(pos: SceneCoord2d)
	{
		mState = JagItemState.Moving;
		mFrameCount = MoveTime;
		mStateFrames = MoveTime;
	}

	
	override public function Update()
	{
		if (mFrameCount > 0) {
			mFrameCount--;
		}
		
		if (mFrameCount == 0) {
			mStateFrames = 0;
			
			switch (mState) {
			case Born:
				mState = JagItemState.Idle;
				
			case Moving:
				mState = JagItemState.Idle;
				
			case Dying:
				mState = JagItemState.Dead;
				
			default:
			}
		}
	}
}
